/**
 * request logic
 */
const NET_CODE_SUC = 200;

/**
 * 判断服务请求是否成功
 * @param response
 * @returns {*|boolean}
 */
export function isResponseSuccess (response) {
  return response && response.code && response.code === NET_CODE_SUC;
}

// 打开新窗口
export function handleOpenNewPageByName (e, routerName, query = {}) {
  const { href } = e.$router.resolve({
    name: routerName,
    query
  });
  window.open(href, '_blank');
}
// 打开新窗口
export function handleOpenNewPageByPath (e, routerPath, query = {}) {
  const { href } = e.$router.resolve({
    path: routerPath,
    query
  });
  window.open(href, '_blank');
}
// 通过表单项生成初始值对象
export const generateFormFieldValues = (list) => {
  const obj = {}
  list.flat().forEach(({ prop, type, multiple, defaultValue }) => {
    const isMultiple = ['select'].includes(type) && multiple
    const isRange = ['dateRangePicker', 'dateTimeRangePicker'].includes(type)
    if (isRange || isMultiple) {
      obj[prop] = []
    } else if (['number', 'select'].includes(type)) {
      obj[prop] = defaultValue || null
    } else {
      obj[prop] = typeof defaultValue !== 'undefined' ? defaultValue : ''
    }
  });
  return obj
}
